package com.git_taoxian.test8;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

public class GoodsManager {
    Scanner sc = new Scanner(System.in);
    ArrayList<Goods> goodsArrayList = new ArrayList<>();
//    活动首页界面
    public void start(){
        System.out.println("JD秒杀....");
//        上架商品信息
        upGoods();
//        商品信息展示
        showGoods();
        System.out.println("请选择需要秒杀的商品：");
        String name = sc.next();
        Goods goods = getGoods(name);
        if (goods==null){
            System.out.println("当前没有该商品对象！");
        }
//        获取当前的时间
        LocalTime time = LocalTime.now();
        if (time.isBefore(goods.getEndTime()) && goods.getBeginTime().isBefore(time)){
            System.out.println("请输入秒杀的数量：");
            int num = sc.nextInt();
            int num1 = goods.getNumber() - num;
            //goods.setNumber(num1);
            if (num1<0){
                System.out.println("当前货品不足，秒杀失败");
            }else{
                System.out.println("秒杀成功！");
            }
        }else{
            System.out.println("当前秒杀时间已过！欢迎参加下次秒杀活动！");
        }



    }
//    获取当前集合中的对象
    public Goods getGoods(String name){
        for (int i = 0; i < goodsArrayList.size(); i++) {
            Goods goods = goodsArrayList.get(i);
            if (goods.getName().equals(name)){
                return goods;
            }
        }
        return null;
    }
//    展示集合对象里面的所有信息
    public void showGoods(){
        for (int i = 0; i < goodsArrayList.size(); i++) {
            Goods goods = goodsArrayList.get(i);
            String s = goods.toString();
            System.out.println("商品序号"+(i+1)+":"+s);
        }
    }
//    上架商品秒杀信息
    public void upGoods() {
        while (true) {
            Goods goods = new Goods();
            System.out.println("请输入上架商品名：");
            String str = sc.next();
            goods.setName(str);
            System.out.println("请输入上架数量：");
            int num = sc.nextInt();
            goods.setNumber(num);
            System.out.println("请输入秒杀时间：（xx:xx:xx）");
            String time1 = sc.next();
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
            LocalTime beginTime = LocalTime.parse(time1, dtf);
            goods.setBeginTime(beginTime);
            System.out.println("请输入秒杀结束时间：（xx:xx:xx）");
            String time2 = sc.next();
            LocalTime endTime = LocalTime.parse(time2, dtf);
            goods.setEndTime(endTime);
            goodsArrayList.add(goods);
            System.out.println("是否需要继续添加商品？输入y表示继续，输入n表示退出上架商品");
            String str2 = sc.next();
            if (str2.equals("y")){

            }else if (str2.equals("n")){
                return;
            }else{
                System.out.println("输入有误，请重新输入：");
                start();
            }
        }
    }
}
